package entity

import (
	dbconnector "YingShop/dbConnector"
	"YingShop/log"
	"YingShop/model"
	"YingShop/utils"
	"errors"
	"fmt"

	"github.com/go-sql-driver/mysql"
	"gorm.io/gorm"
)

type ProductCategory struct {
	CategoryId   int
	CategoryName string
	CreateTime   string
	UpdateTime   string
	Creator      string
	Modifier     string
}

func (pCategory *ProductCategory) Load() error {
	conn := dbconnector.GetDb()
	categoryModel := model.ProductCategory{}
	result := conn.First(&categoryModel, pCategory.CategoryId)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			return fmt.Errorf("找不到id为%d的分类", pCategory.CategoryId)
		} else {
			log.Error(result.Error.Error())
			return errors.New("未知错误")
		}
	}
	pCategory.CategoryName = categoryModel.CategoryName
	pCategory.CreateTime = utils.TimeFormatToNormal(categoryModel.CreateTime)
	pCategory.UpdateTime = utils.TimeFormatToNormal(categoryModel.UpdateTime)
	pCategory.Creator = categoryModel.Creator
	pCategory.Modifier = categoryModel.Modifier
	return nil
}

func (ProductCategory *ProductCategory) Add() error {
	if ProductCategory.CategoryId != 0 {
		return fmt.Errorf("商品分类id不为0，不可新增")
	}
	categoryModel := model.ProductCategory{
		CategoryName: ProductCategory.CategoryName,
		Creator:      ProductCategory.Creator,
		Modifier:     ProductCategory.Modifier,
	}
	conn := dbconnector.GetDb()
	sameNameCategoryModel := model.ProductCategory{}
	result := conn.Where("category_name", categoryModel.CategoryName).First(&sameNameCategoryModel)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			result = conn.Create(&categoryModel)
			if result.Error != nil {
				if errMySQL, ok := result.Error.(*mysql.MySQLError); ok {
					switch errMySQL.Number {
					case 1062:
						conn.Rollback()
						return fmt.Errorf("名称为%s的分类已存在,请重新输入", categoryModel.CategoryName)
					default:
						log.Error(fmt.Sprintf("新增分类%s时发生错误%s", categoryModel.CategoryName, result.Error))
						conn.Rollback()
						return fmt.Errorf("新增商品时发生未知错误")
					}
				}
			}
			ProductCategory.CategoryId = categoryModel.CategoryId
		} else {
			log.Error(fmt.Sprintf("创建分类%s时发生错误%s", categoryModel.CategoryName, result.Error))
			return fmt.Errorf("创建分类时发生未知错误")
		}

	} else {
		return fmt.Errorf("名称为%s的分类已存在,请重新输入", sameNameCategoryModel.CategoryName)
	}
	return nil
}

type ProductCategoryList struct {
	CategoryList []ProductCategory
}

func (categoryList *ProductCategoryList) GetAllCategory() {
	categoryModelList := make([]model.ProductCategory, 0)
	conn := dbconnector.GetDb()
	conn.Find(&categoryModelList)
	for _, value := range categoryModelList {
		categoryEntity := ProductCategory{}
		categoryEntity.CategoryId = value.CategoryId
		categoryEntity.CategoryName = value.CategoryName
		categoryEntity.Creator = value.Creator
		categoryEntity.Modifier = value.Modifier
		categoryEntity.CreateTime = utils.TimeFormatToNormal(value.CreateTime)
		categoryEntity.UpdateTime = utils.TimeFormatToNormal(value.UpdateTime)
		categoryList.CategoryList = append(categoryList.CategoryList, categoryEntity)
	}
}
